/*
 * Progetto, a new java application 
 *
 * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.  All third-party contributions are
 * distributed under license by Red Hat Inc.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Rattazzi Street, Fifth Floor
 * Pomezia, RM  00040  Italy
 */
package com.jfootball.business.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jfootball.Constant;
import com.jfootball.dao.PlayerDao;
import com.jfootball.dao.TeamDao;
import com.jfootball.domain.Player;
import com.jfootball.domain.Team;
import com.jfootball.service.PlayerService;

/**
 * @author C_ICTDNS
 *
 */
@Transactional(readOnly = true)
@Service("playerService")
@RemoteProxy(name = "playerService")
public class PlayerServiceImpl implements PlayerService
{
	private PlayerDao playerDAO;
	private TeamDao teamDAO;	
	
	
	@Autowired
	public PlayerServiceImpl(PlayerDao playerDAO, TeamDao teamDAO) 
	{
		this.playerDAO = playerDAO;
		this.teamDAO = teamDAO;
	}		

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void savePlayer(Player obj)
	{
		if (obj.getImage() == null && obj.getId() != null)
		{
			Long id = obj.getId();
			Player p = getPlayerByID(id);
			byte[] image = p.getImage();
			obj.setImage(image);
		}	
		playerDAO.savePlayer(obj);
	}

	
	public List<Player> getPlayersByTeam(Long idTeam, String teamCategory)
	{
		return playerDAO.listPlayersByTeam(idTeam, teamCategory);
	}
	
	public List<Player> getPlayersByLetter(String letter, String searchType)
	{
		return playerDAO.listPlayersByLetter(letter, searchType);
	}	
	

	public List<Player> getPlayersByDivision(Long idDivision)
	{
		List<Player> listaOrig = getPlayers();
		List<Player> listaNew = new ArrayList<Player>();
		for (Player player : listaOrig)
		{
			if (player.getTeam().getId() != null )
			{
				listaNew.add(player);
			}
		}
		return listaNew;
	}	
	

	public List<Player> getPlayers()
	{
		return playerDAO.listPlayers();
	}


	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void deletePlayer(Long idPlayer)
	{
		playerDAO.deletePlayer(idPlayer);
	}

	public Player getPlayerByID(Long idPlayer)
	{
		Player p = playerDAO.getPlayerById(idPlayer);
		return p;
	}
	
	@Transactional(readOnly = false)
	public void updatePlayer(String playerId, String teamId, String isTeamOwner, String status)
	{
		Player player = getPlayerByID(new Long(playerId));
		Team team = teamDAO.getTeamByID(new Long(teamId));
		Team teamPrev = player.getTeam();
		
		if (! new Boolean(isTeamOwner)){
			player.setTeamOwner(team);
			player.setStatus(status);
		}
		else
			player.setStatus(Constant.ON_LOAN);

		
		player.setNumber(null);
		player.setRetired(false);
		player.setUnemployed(false);
		
		
		player.setTeam(team);
		player.setTeamPrev(teamPrev);
		playerDAO.savePlayer(player);
		
	}
	
	public boolean findPlayerExists(String firstName, String lastName, String dateOfBirth)
	{
		return playerDAO.findPlayerExists(firstName, lastName, dateOfBirth);
	}
	

	public HashMap<String, Object> getHashMap(Long teamId, Integer rankId)
	{
		return playerDAO.getNextId(teamId, rankId);
	}
	

	public String getRank(Long teamId, Long playerId) 
	{
		return playerDAO.getRank(teamId, playerId);		
	}
	
	public void doEndSeasonJob()
	{
		playerDAO.endSeasonJob();		
	}
	
	
	public void doCareerPlayerJob()
	{
		playerDAO.careerPlayerJob();
	}

	
	

	
}
